<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods,i) in goodsList " :key="i" @click="gotoDetail(goods)">
        <!-- 为 my-goods 组件动态绑定goods属性的值 -->
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          // 查询关键字
          query: '',
          // 商品分类id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        // 商品列表数据
        goodsList: [],
        // 总数量
        total: 0
      };
    },
    onLoad(options) {
      //将页面参数转存到this.queryObj对象中
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      console.log(this.queryObj)
      //调用获取商品列表数据的方法
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        console.log(res)
        cb&&cb()
        // 请求失败
        if (res.meta.status !== 200)
          return uni.$showMsg()
        // 为数据赋值
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(goods){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+goods.goods_id
        })
      }
    },
    //触底事件
    onReachBottom(){
      //让页码值自增 +1
      this.queryObj.pagenum +=1
      // 重新获取列表数据
      this.getGoodsList()
    },
    // 下拉刷新事件
    onPullDownRefresh(){
      // 1.重置关键数据
      this.queryObj.pagenum=1
      this.goodsList=[]
      this.total=0
      //重新发起数据请求 
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
